const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');
const user = require('../../../../services/user.js');
const common = require('../../../../utils/common.js');
const app = getApp()
Page({
  data: {
    //店铺展示的类型
    openPanel: 0,
    //功能面板
    openFunction: false,
    //公告栏面板
    showNotice: false,
    //公告编辑控制
    openNoticeUpdate: false,
    //公告栏内如
    noticeText: '',
    //设置海报面板
    showSetPoster: false,
    //设置海报展示的类型
    openSetPosterPanel: 0,
    //设置海报圈子数据
    circleArr: '',
    addBabyselectCircle: false,//添加宝贝选择圈子面板
    joinCircleList: [],//用户创建的圈子
    selectCircle: null,//添加宝贝选中的圈子
  },
  onLoad: function (option) {
    var _this = this;
    _this.setData({
      logininfo: app.globalData.logininfo,
      openid: app.globalData.logininfo.openid,
    });
    //判断是不是用户本人访问
    if (option.openid != null && option.openid != '') {
      _this.setData({
        getopenid: option.openid
      })
    } else {
      _this.setData({
        getopenid: app.globalData.logininfo.openid
      })
    }
  },
  onReady: function () {
    var _this = this
    var openid = _this.data.openid
    var getopenid = _this.data.getopenid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = getopenid;
        data.access_token = res.access_token;
        //获取用户信息
        util.post(api.GetUserInfoByHomepage, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              userData: res.result.json,
            })
            console.log(_this.data.userData)
          }
        });
        //获取用户资料
        util.post(api.GetHomeInfo, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              userData2: res.result.entity
            })
            console.log(_this.data.userData2)
          }
        });
        var data = {};
        data.openid = openid;
        data.selectOpenid = getopenid;
        data.access_token = res.access_token;
      });
    }
  },
  onShow: function () {
    var _this = this
    if (app.switchData != '' && app.switchData !=null) {
      _this.setData({
        openPanel: app.switchData
      });
      app.switchData = '';
    }
  },
  //选择浏览的类型
  selectPanel: function (e) {
    var _this = this
    var openid = _this.data.openid
    var getopenid = _this.data.getopenid;
    var index = e.currentTarget.dataset.index
    _this.setData({
      openPanel: index
    })
    if (index == 0) {
      if (openid != null && openid != "") {
        util.post(api.fetchToken()).then(function (res) {
          if (res.access_token != '') {
            return Promise.resolve({
              access_token: res.access_token
            });
          }
        }).then(function (res) {
          var data = {};
          data.openid = openid;
          data.selectOpenid = getopenid;
          data.access_token = res.access_token;
        });
      }
    }
  },
  //选择设置海报的类型
  selectSetPosterPanel: function (e) {
    var _this = this
    var openid = _this.data.openid
    var getopenid = _this.data.getopenid;
    var index = e.currentTarget.dataset.index
    _this.setData({
      openSetPosterPanel: index
    })
    if (index == 0) {
      if (openid != null && openid != "") {
        util.post(api.fetchToken()).then(function (res) {
          if (res.access_token != '') {
            return Promise.resolve({
              access_token: res.access_token
            });
          }
        }).then(function (res) {
          var data = {};
          data.openid = openid;
          data.selectOpenid = getopenid;
          data.access_token = res.access_token;
          util.post(api.Home_ParticipateCircleApiUrl, data).then(function (res2) {
            if (res2.code == 200) {
              var arr = []
              for (var i = 0; i < res2.result.list.length; i++) {
                if (res2.result.list[i].identityType == 1) {
                  arr.push(res2.result.list[i])
                }
              }
              _this.setData({
                circleArr: arr,
              })
              console.log(arr)
            }
          })
        });
      }
    }
  },
  //打开公告栏面板
  openNoticePanel: function () {
    var _this = this
    _this.setData({
      showNotice: true
    })
  },
  //开启编辑状态
  showNoticeUpdate: function () {
    var _this = this
    _this.setData({
      openNoticeUpdate: true
    })
  },
  //关闭编辑状态
  closeNoticeUpdate: function () {
    var _this = this
    _this.setData({
      openNoticeUpdate: false
    })
  },
  //获取编辑内容
  inputNoticeText: function (e) {
    var _this = this
    var value = e.detail.value.replace(/\s+/g, '')
    _this.setData({
      noticeText: value,
    })
  },
  //打开更多功能面板
  showFunction: function () {
    var _this = this
    _this.setData({
      openFunction: true
    })
  },
  //打开设置海报面板
  openSetPoster: function () {
    var _this = this
    var openid = _this.data.openid
    var getopenid = _this.data.getopenid;
    _this.setData({
      showSetPoster: true,
      openFunction: false,
      openSetPosterPanel: 0
    })
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = openid;
        data.selectOpenid = getopenid;
        data.access_token = res.access_token;
        util.post(api.Home_ParticipateCircleApiUrl, data).then(function (res2) {
          if (res2.code == 200) {
            var arr = []
            for (var i = 0; i < res2.result.list.length; i++) {
              if (res2.result.list[i].identityType == 1) {
                arr.push(res2.result.list[i])
              }
            }
            _this.setData({
              circleArr: arr,
            })
            console.log(arr)
          }
        })
      });
    }
  },
  //打开二维码面板
  openQRcode: function () {
    var _this = this
    _this.setData({
      showQPcode: true,
      openFunction: false,
    })
  },
  //关闭面板
  onClose: function () {
    var _this = this
    _this.setData({
      openFunction: false,
      showNotice: false,
      showSetPoster: false,
      showQPcode: false,
      addBabyselectCircle: false,
    })
  },
  //添加一个新的兴趣圈子
  doPublish: function () {
    wx.navigateTo({
      url: '/ucenter/pages/ucenter/personcenter/publish/publish',
    })
  },
  /**
   * 新增宝贝
   */
  addBaby: function () {
    var _this = this
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.selectOpenid = openid;
        data.access_token = res.access_token;
        util.post(api.Home_ParticipateCircleApiUrl, data).then(function (res2) {
          if (res2.code == 200) {
            var arr = []
            for (var i = 0; i < res2.result.list.length; i++) {
              if (res2.result.list[i].identityType == 1) {
                arr.push(res2.result.list[i])
              }
            }
            _this.setData({
              joinCircleList: arr,
              addBabyselectCircle: true
            })
            console.log(arr)
          }
        })
      });
    }
  },
  /**
   * 选择添加宝贝绑定的圈子
   */
  clickCircle: function (e) {
    var _this = this;
    var index = e.currentTarget.dataset.index
    _this.setData({
      selectCircle: index+1
    })
  },
  /**
   * 进入管理主题
   */
  gotoThemeManage: function () {
    var _this = this;
    var selectCircle = _this.data.selectCircle-1;
    var arr = _this.data.interestJoinList;
    var circleId = arr[selectCircle].circleid
    var _this = this;
    wx.navigateTo({
      url: "/interest/pages/interest/theme/themManage/themManage?circleid=" + circleId
    })
    _this.setData({
      addBabyselectCircle: false
    })
  },
})